package com.mingyuan.library.domain.lending.book.model.entity;

import com.mingyuan.library.domain.common.model.valueobject.Version;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookId;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookInformation;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookType;
import com.mingyuan.library.domain.lending.librarybranch.model.valueobject.LibraryBranchId;
import com.mingyuan.library.domain.lending.patron.event.BookReturned;
import com.mingyuan.library.domain.lending.patron.model.valueobject.PatronId;
import lombok.*;

@Value
@AllArgsConstructor
@EqualsAndHashCode(of = "bookInformation")
public class CheckedOutBook implements Book {

    @NonNull
    BookInformation bookInformation;

    @NonNull
    LibraryBranchId checkedOutAt;

    @NonNull
    PatronId byPatron;

    @NonNull
    Version version;

    public CheckedOutBook(BookId bookId, BookType type, LibraryBranchId libraryBranchId, PatronId patronId, Version version) {
        this(new BookInformation(bookId, type), libraryBranchId, patronId, version);
    }

    public BookId getBookId() {
        return bookInformation.getBookId();
    }

    /**
     * 归还图书处理
     * @author yangzk
     * @param bookReturnedByPatron 图书归还事件
     * @date 2024/1/16 13:35
     **/
    public AvailableBook handle(BookReturned bookReturnedByPatron) {
        return new AvailableBook(
                bookInformation,
                new LibraryBranchId(bookReturnedByPatron.getLibraryBranchId()),
                version);
    }

}

